package com.difsonapps.contacttranslator.ws.rest;

import java.util.Random;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.difsonapps.contacttranslator.component.MailService;
import com.difsonapps.contacttranslator.dto.MessageDTO;
import com.difsonapps.contacttranslator.dto.TransactionSourceTypeEnum;
import com.difsonapps.contacttranslator.dto.UserDTO;
import com.difsonapps.contacttranslator.dto.UserTransactionDTO;
import com.difsonapps.contacttranslator.service.itf.UserService;
import com.difsonapps.contacttranslator.service.itf.UserTransactionService;
import com.difsonapps.contacttranslator.utils.CommonProps;
import com.difsonapps.contacttranslator.utils.Constant;
import com.difsonapps.contacttranslator.utils.FCMessage;
import com.difsonapps.contacttranslator.utils.Utils;

@Controller
@RequestMapping("/notification")
public class NotificationCtrl {

	@Autowired
	UserService userService;
	@Autowired
	UserTransactionService userTransactionService;
	@Autowired
	CommonProps commonProps;

	Logger logger = Logger.getLogger(getClass());


	@RequestMapping(method = RequestMethod.POST, value = "mtn/phonenumber/{phonenumber}/amount/{amount}/message/{message}/usercode/{usercode}")
	public @ResponseBody
	boolean confirmUser(@PathVariable String phonenumber,
			@PathVariable double amount, @PathVariable String message,
			@PathVariable int usercode) {
		try{
		if(!phonenumber.startsWith(commonProps.prefixNumber))
		{
			phonenumber = commonProps.prefixNumber+phonenumber;
		}
		UserDTO userDTO = userService.get(usercode);
		if(userDTO!=null)
		{
			userDTO = userService.getByNumber(phonenumber);
		}
		UserTransactionDTO userTransactionDTO = new UserTransactionDTO();
		userTransactionDTO.setAccount(phonenumber);
		userTransactionDTO.setAmount(amount);
		userTransactionDTO.setMessageconfirm(message);
		userTransactionDTO.setUserDTO(userDTO);
		userTransactionDTO.setSource(TransactionSourceTypeEnum.MOBILE_MONEY);

		userTransactionService.insert(userTransactionDTO);
		}catch(Exception e)
		{
			logger.error(e.getMessage());
			return false;
		}
		return true;
	}

}
